package org.github.choviwu.example.common.web;

import org.apache.shiro.authz.AuthorizationException;
import org.github.choviwu.example.common.exception.CrudException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常
 */
@RestControllerAdvice(basePackages = "org.choviwu")
public class ExceptionController {

    private final HttpServletRequest request;

    @Autowired
    ExceptionController(HttpServletRequest request){
        this.request = request;
    }
    @ExceptionHandler(CrudException.class)
    private Object exceptionDeal(CrudException ex){
        Map map = new HashMap();
        map.put("result","fail");
        map.put("message",ex.getMessage());
        map.put("code",1);
        return map;
    }

    //返回403
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerExceptionForBidden(){}

    //未授权 没权限
    @ExceptionHandler(value = AuthorizationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public void handlerExceptionUnAuthorized(){}


}
